<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CMLAdmin extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('cmladministration','refresh');
	    }
	    /*elseif ($_SESSION['role_id'] !=1){
    		redirect('memberlogin/restriction','refresh');
	    }
    	$data['menus'] = 'menus';
		$this->load->vars($data);*/
        $this->load->model('MPage');
        $this->load->model('MCategory');
        $this->load->model('MSetting');
        $this->tinyMce = '
		<!-- TinyMCE -->
		<script type="text/javascript" src="'. base_url().'media/admin/js/tiny_mce/tiny_mce.js"></script>
		<script type="text/javascript">
	 		tinyMCE.init({
        		// General options
        		mode : "textareas",
        		theme : "advanced",
        		plugins : "openmanager,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,visualblocks",
        
        		// Theme options
        		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor,|,openmanager",
                theme_advanced_buttons3 : "tablecontrols",
        		theme_advanced_toolbar_location : "top",
        		theme_advanced_toolbar_align : "left",
        		theme_advanced_statusbar_location : "bottom",
        		theme_advanced_resizing : true,
                
                table_styles : "Header 1=header1;Header 2=header2;Header 3=header3",
            	table_cell_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Cell=tableCel1",
            	table_row_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",
            	table_cell_limit : 100,
            	table_row_limit : 50,
            	table_col_limit : 50,
        		
        		//Open Manager Options
        		file_browser_callback: "openmanager",
        		open_manager_upload_path: "../../../uploads/",
        
        		// Example content CSS (should be your site CSS)
        		content_css : "/media/css-global/custom.css",
        
        		// Drop lists for link/image/media/template dialogs
        		template_external_list_url : "lists/template_list.js",
        		external_link_list_url : "lists/link_list.js",
        		external_image_list_url : "lists/image_list.js",
        		media_external_list_url : "lists/media_list.js",
        
        		// Style formats
        		style_formats : [
        			{title : "Bold text", inline : "b"},
        			{title : "Red text", inline : "span", styles : {color : "#ff0000"}},
        			{title : "Red header", block : "h1", styles : {color : "#ff0000"}},
        			{title : "Example 1", inline : "span", classes : "example1"},
        			{title : "Example 2", inline : "span", classes : "example2"},
                    {title : "History Paragraph", inline : "p", classes : "teswtafdd"},
        			{title : "Table styles"},
        			{title : "Table row 1", selector : "tr", classes : "tablerow1"}
        		],
                 
        		// Replace values for the template plugin
        		template_replace_values : {
        			username : "Some User",
        			staffid : "991234"
        		}
        	});
		</script>
		<!-- /TinyMCE -->
		';
        
        /* 
        $this->tinyMce = '
		<!-- TinyMCE -->
		<script type="text/javascript" src="'. base_url().'media/admin/js/tinymce/tinymce.js"></script>
		<script type="text/javascript">
	 		tinymce.init({
            selector: "textarea",
            theme: "modern",
            plugins: [
                "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                "searchreplace wordcount visualblocks visualchars code fullscreen",
                "insertdatetime media nonbreaking save table contextmenu directionality",
                "emoticons template paste textcolor moxiemanager"
            ],
            toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link code image | forecolor backcolor emoticons",
            image_advtab: true,
            statusbar : false,
            menubar : false,
            force_p_newlines : false,
            force_br_newlines : true,
            convert_newlines_to_brs : true,
            forced_root_block : false,
            paste_text_linebreaktype : "br",
            width : 730,
            height : 300,
            templates: [
            {title: "Test template 1", content: "Test 1"},
            {title: "Test template 2", content: "Test 2"}
            ]
        });
		</script>
		<!-- /TinyMCE -->
		';
        */
    }
	    
	public function index()
	{
		$data['title'] = "Dashboard - Chamroeun Microfinance Limited";
		$data['main'] = 'admin/dashboard';
		$this->load->vars($data);
		$this->load->view('admin/layout'); 
	}
    
	public function pageList()
	{
		$data['title'] = 'Page List';
		$data['main'] = 'admin/pagelist';
        if($_SESSION['role_id'] == 1)
        {
            $data['pages'] = $this->MPage->getAllPages();
        }
        else
        {
            $data['pages'] = $this->MPage->getAllAssignedPages($_SESSION['role_id']);
        }     
		$this->load->vars($data);
		$this->load->view('admin/layout');
	}
	public function pageAdd()
	{
	   if($this->input->post('title'))
       {
            $this->MPage->addEditPage();
	  		$this->session->set_flashdata('message','Page added');
	  		redirect('pagelist','refresh');
       }
		$data['title'] = "Add New Page";
		$data['main'] = 'admin/pageadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('pagelist');
        $data['categorylist'] = $this->MCategory->getCategoryDropdown();
        $data['pagelist'] = $this->MPage->getPageDropdown();
        $data['khcode'] = $this->MSetting->getLanguageActiveCode('kh');
		$this->load->vars($data);
		$this->load->view('admin/layout'); 
	}
    
    public function pageEdit($page_id = 0)
	{
	   if($this->input->post('title'))
       {
            $this->MPage->addEditPage($this->input->post('page_id'));
	  		$this->session->set_flashdata('message','Page edited');
            if($this->input->post('submit_close'))
                redirect('pagelist','refresh');
            else
                redirect('cmladmin/pageedit/'.$this->input->post('page_id'),'refresh');
       }
		$data['title'] = "Edit Page";
		$data['main'] = 'admin/pageedit';
        $data['form'] = $this->MFormGlobal->getButtonsEdit('pagelist','cmladmin/pagedelete/'.$page_id);
        $psetting = $this->MPage->getPageSettingByPageIdRoleId($page_id, $_SESSION['role_id']);
        if($psetting != null && $psetting == 1){
            $data['form'] = $this->MFormGlobal->getButtonsEditDisableDel('pagelist','?#');
        }
        $data['categorylist'] = $this->MCategory->getCategoryDropdown();
        $data['pagelist'] = $this->MPage->getPageEditDropdown($page_id);
        $data['page'] = $this->MPage->getPageById($page_id);
        $data['khcode'] = $this->MSetting->getLanguageActiveCode('kh');
		$this->load->vars($data);
		$this->load->view('admin/layout'); 
	}
    
    public function pageDelete($page_id = 0)
	{
	   $this->MPage->deletePage($page_id);
	   $this->session->set_flashdata('message','Page deleted');
	   redirect('pagelist','refresh');
    }
    
    public function accessDenied()
    {
        $data['title'] = 'Access Denied';
		$data['main'] = 'admin/accessdenied';     
		$this->load->vars($data);
		$this->load->view('admin/layout');  
    }
}
